<template>
	<dt-main-body v-model="labelPosition">
		<div class="location">
			<el-breadcrumb separator-class="el-icon-arrow-right">
				<el-breadcrumb-item>
					<i class="iconfont icon-home"></i>
					管理中心
				</el-breadcrumb-item>
				<el-breadcrumb-item>系统功能</el-breadcrumb-item>
				<el-breadcrumb-item>编辑授权</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		
		<div class="content-box">
			<el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
				<el-tabs v-model="activeName" type="card">
					<el-tab-pane label="基本信息" name="info">
						<div class="tab-content">
							<el-form-item label="是否启用">
								<el-switch :active-value="0" :inactive-value="1" v-model="model.status"></el-switch>
							</el-form-item>
							<el-form-item prop="siteId" label="所属站点">
								<el-select v-model="model.siteId" placeholder="请选择...">
									<el-option v-for="item in siteList"
										:key="item.id"
										:label="item.title"
										:value="item.id">
									</el-option>
								</el-select>
							</el-form-item>
							<el-form-item prop="provider" label="开放平台">
								<el-radio-group v-model="model.provider">
									<el-radio-button label="qq">QQ互联</el-radio-button>
									<el-radio-button label="wechat">微信</el-radio-button>
								</el-radio-group>
							</el-form-item>
							<el-form-item prop="type" label="应用类型">
								<el-radio-group v-model="model.type">
									<el-radio-button label="web">网页</el-radio-button>
									<el-radio-button label="h5">H5</el-radio-button>
									<el-radio-button label="mp">小程序</el-radio-button>
									<el-radio-button label="app">APP应用</el-radio-button>
								</el-radio-group>
							</el-form-item>
							<el-form-item label="显示图标">
								<dt-upload v-model="model.imgUrl"></dt-upload>
							</el-form-item>
							<el-form-item prop="title" label="标题名称">
								<el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
							</el-form-item>
							<el-form-item prop="clientId" label="AppId">
								<el-input v-model="model.clientId" placeholder="必填，开放平台提供的AppId"></el-input>
							</el-form-item>
							<el-form-item prop="clientSecret" label="AppSecret">
								<el-input v-model="model.clientSecret" placeholder="必填，开放平台提供的AppKey"></el-input>
							</el-form-item>
							<el-form-item prop="sortId" label="排序数字">
								<el-input v-model="model.sortId" placeholder="数字越小越排前"></el-input>
							</el-form-item>
							<el-form-item label="创建时间" v-if="model.id>0">
								{{model.addBy}} {{model.addTime}}
							</el-form-item>
						</div>
					</el-tab-pane>
				</el-tabs>
			</el-form>
		</div>
		
		<div class="footer-box">
			<div class="footer-btn">
				<el-button type="primary" @click="submitForm">确认保存</el-button>
				<el-button plain @click="$common.back()">返回上一页</el-button>
			</div>
		</div>
	</dt-main-body>
</template>

<script>
	import DtUpload from '../../../components/DtUpload.vue'
	
	export default {
		components: {
			DtUpload
		},
		props: {
			id: 0
		},
		data() {
			return {
				activeName: 'info', //当前选项卡
				labelPosition: 'right', //表单对齐方式
				siteList: [],
				model: {
					id: 0,
					siteId: null,
					provider: null,
					type: null,
					title: null,
					imgUrl: null,
					clientId: null,
					clientSecret: null,
					sortId: 99,
					status: 0,
					addBy: null,
					addTime: null
				},
				rules: {
					siteId: [
						{ required: true, message: '请选择所属站点', trigger: 'change' }
					],
					provider: [
						{ required: true, message: '请选择接入平台', trigger: 'change' }
					],
					type: [
						{ required: true, message: '请选择应用类型', trigger: 'change' }
					],
					title: [
						{ required: true, message: '请输入标题名称', trigger: 'blur' }
					],
					clientId: [
						{ required: true, message: '请输入开放平台提供的AppId', trigger: 'blur' }
					],
					clientSecret: [
						{ required: true, message: '请输入开放平台提供的AppSecret', trigger: 'blur' }
					],
					sortId: [
						{ required: true, message: '请输入排序数字', trigger: 'blur' }
					]
				}
			}
		},
		methods: {
			//初始化数据
			initData() {
				let _this = this; //当前页面
				//加载站点列表
				_this.$api.request({
					url: '/admin/site/view/0',
					success(res) {
						_this.siteList = res.data
					}
				})
				//赋值表单
				if (_this.id) {
					_this.$api.request({
						url: '/admin/site/oauth/' + _this.id,
						loading: true,
						success(res) {
							//赋值给model
							Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
						}
					});
				}
			},
			//提交表单
			submitForm() {
				let _this = this; //当前页面
				//多个表单验证
				const f1 = new Promise((resolve, reject) => {
					_this.$refs['editForm'].validate((valid) => {
						if (valid) resolve()
					});
				});
				Promise.all([f1]).then(() => {
					//提交表单,Id大于0则修改，否则添加
					if (_this.model.id > 0) {
						this.$api.request({
							method: 'put',
							url: '/admin/site/oauth/' + _this.model.id,
							data: _this.model,
							loading: true,
							successMsg: '修改授权平台已成功',
							success(res) {
								//跳转列表页
								_this.$common.linkUrl('/setting/oauth/list');
							}
						});
					} else {
						this.$api.request({
							method: 'post',
							url: '/admin/site/oauth',
							data: _this.model,
							loading: true,
							successMsg: '新增授权平台已成功',
							success(res) {
								//重置表单
								_this.$refs['editForm'].resetFields();
							}
						});
					}
				})
			},
		},
		created() {
			this.initData();
		}
	}
</script>